<?php
class model_worklog extends Model
{
	function __construct()
	{
		parent::__construct();
	}

	
	function newWorklog($jobId,$techId)
	{
		$data = array(	"job_id" => $jobId 
						,"tech_id" => $techId
						,"created_by" => $_SESSION['user_id']
						);
		$this->db->insert("worklog", $data);
		
	}
	
	function updateWorkLog($data, $id)
	{
		$this->db->where("id", $id);
		$this->db->update("worklog", $data);
	}
	
	function getWorklogs($limit=20,$order = array("wl.created", "desc"), $where = array())
	{
		$this->db->select("wl.*,
							,concat_ws(' ', u.firstname,  u.lastname ) as fullname
							,u.username
							,u.id as user_id
							,j.id as job_id
							,j.job_start
							,j.job_subject
							,lu.lu_lable as job_status
							,c.company_name
							,c.id as company_id
						")
				->from("worklog wl")
				->join("job j", "j.id=wl.job_id")
				->join("user u", "u.id=tech_id") //userId == tech_id
				->join("company c", "j.company_id=c.id")
				->join("lookup lu", "j.job_status=lu.lu_value and lu.lu_type='job_status'")
				->limit($limit)
				->order_by($order[0], $order[1])
				;
		if(!empty($where))
		{
			$this->db->where($where);
		}
		$r = $this->db->get();
		return $r->result_array();
		
		
	
		
	}
	
	function activate($id)
	{
		$data = array("active" =>1);
		$this->db->where("id", $id);
		$this->db->update("worklog", $data);
	}

	function deactivate($id)
	{
		$data = array("active" =>0);
		$this->db->where("id", $id);
		$this->db->update("worklog", $data);
	}
}